package client.gui;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import java.util.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;


import messages.*;
import sharedspace.*;



/**
 * This JPanel is a panel for Teacher to send message to the manager and ask for extension
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Teacher_Extension_Request extends GUI_AbstractMainMenu {

	public Client_GUI ms=null;
	public JButton btnBack = null;
	public JButton reqTimeBtn = null;
	private JTable exJTable = null;
	private List<ExecuteTest> exList = null;
	private List<Executetestinfo> exInfoList = null;
	private JScrollPane exScroller = null;
	private Timestamp nowTime;
	/**
	 * This is the default constructor
	 * @param MS
	 */
	public GUI_Teacher_Extension_Request(Client_GUI MS) {
		super(MainMenuType.GUEST,MS);

		this.ms=MS;
		greetMsg.setText("Time Extention Request");
		mainMenu.setText("Teacher");
		initiate();
	}

	/**
	 * This method initializes the screen
	 */
	private void initiate() {
			
		this.setLayout(null);
		this.background.add(getbtnBack());
		this.background.add(getReqTimeBtn());
		this.background.add(getExTable());

	}
	
	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}

	private JButton getReqTimeBtn()
	{
		if (reqTimeBtn == null)
		{
			reqTimeBtn = new JButton("Request Time Extention");	
			reqTimeBtn.setBounds(400, 600, 150, 30);
			reqTimeBtn.setVisible(true);

			reqTimeBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (exJTable.getSelectedRow() != -1)
					{	
						String timeAdd = JOptionPane.showInputDialog("How much time do you want to add (number only) ?");

						if (timeAdd == null){
							JOptionPane.showMessageDialog(null, "Request Canceled");
							return;	
						}
						else if (timeAdd.isEmpty()){
							JOptionPane.showMessageDialog(null, "Request Canceled");
							return;	
						}
						
				
						try
						{
							int i = Integer.parseInt(timeAdd);
							if (i<1){
								JOptionPane.showMessageDialog(null, "Time Extention must be a positive number!");
								return;	
							}
							String reasonAdd = JOptionPane.showInputDialog("Why is this extention neccessry?");
							if (reasonAdd.isEmpty()) {
								JOptionPane.showMessageDialog(null, "A reason for the time extention request MUST be explained!");
								return;
							}
							String tID = exList.get(exJTable.getSelectedRow()).getExecuteTestID();
							MessageSendTimeExtentionRequest MsgSter	= new MessageSendTimeExtentionRequest(i, reasonAdd, tID, ms.lg.getUser());

							try {
								ms.client.sendToServer(MsgSter);
								JOptionPane.showMessageDialog(null, ms.client.GetMessage());
							} catch (IOException e1) {
								e1.printStackTrace();
							}
						}
						catch (NumberFormatException err)
						{
							JOptionPane.showMessageDialog(null, "Extra Time must contain numbers only!");
						}
						catch (NullPointerException err)
						{
							JOptionPane.showMessageDialog(null, "Request Canceled!");
						}
					}
					else JOptionPane.showMessageDialog(null, "No test has been selected!");
				}
			});
		}
		
		return reqTimeBtn;
	}

	private JScrollPane getExTable()
	{
		if (exJTable == null)
		{
			MessageExecutedExamListByTeacherID msgGel=new MessageExecutedExamListByTeacherID(ms.lg.getUser().getUserID());
			try {
				ms.client.sendToServer(msgGel);
			} catch (IOException e) {
				e.printStackTrace();
			}
			MessageExecutedExamListReply MsgExlrep = (MessageExecutedExamListReply)ms.client.GetMessage();
			
			exList = MsgExlrep.getExTestList();
			exInfoList = MsgExlrep.getExTestInfoList();
			
			//TODO Move this to server
			String now = DateUtils.now(DateUtils.dateFormat);
			SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.dateFormat);
			Date date;

			try {
				date = dateFormat.parse(now);
				nowTime = new Timestamp(date.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			for (int i = exList.size()-1; i>=0; i--){
				if ( (exInfoList.get(i).getStartTime().after(nowTime)) || (exList.get(i).getTestStatus() == false) ){
					exList.remove(i);
					exInfoList.remove(i);
				}

			}
			
			String columnNames[] = {"Subject","Course","Year","Semester"};
			String tableModel[][] = new String[exList.size()][Question.NUM_OF_COLUMNS-5];
			for (int i=0; i<exList.size(); i++){
				tableModel[i][0] = exList.get(i).getTest().getSubject().getSubjectName();
				tableModel[i][1] = exList.get(i).getTest().getCourse().getCourseName();
				tableModel[i][2] = "" + exList.get(i).getTestYear();
				tableModel[i][3] = "" + exList.get(i).getTestSemster();
			}
			exJTable = new JTable(tableModel, columnNames){
				   public boolean isCellEditable(int rowIndex, int colIndex) {
				   return false; //Disallow the editing of any cell
				   }
				   };		 
			exJTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
			exJTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			exJTable.getTableHeader().setReorderingAllowed(false);
			//exJTable.setRowSelectionInterval(-1, -1);
			exScroller= new JScrollPane(exJTable);
			exScroller.setBounds(50, 150, 900, 400);
		}
			
		
		return exScroller;
	}
	
}  
